﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using VonaJobs.DAL.DAO;

namespace VonaJobs.DAL.Gatway
{
    public class LocationGateway:DBGateway
    {
        public string SaveNewLocation(Location location)
        {
            try
            {
                ConnectionObj.Open();
                CommandObj.CommandText =
                    string.Format("insert into tblLocation values(@Country,@City)");
                CommandObj.Parameters.Clear();
                CommandObj.Parameters.Add("@Country", SqlDbType.NVarChar).Value = location.Country;
                CommandObj.Parameters.Add("@City", SqlDbType.NVarChar).Value = location.City;
                CommandObj.ExecuteNonQuery();
                return "New location created successfully";
            }
            catch (Exception)
            {
                throw new Exception("Message Not Send, Server Error !");
            }
            finally
            {
                if (ConnectionObj != null && ConnectionObj.State == ConnectionState.Open)
                {
                    ConnectionObj.Close();
                }

            }
        }

        public bool HasThisjobLocationExist(Location location)
        {
            try
            {
                ConnectionObj.Open();
                string select = String.Format("Select * From tblJobLocation Where Country='{0}' and city='{1}'",location.Country,location.City);
                CommandObj.CommandText = select;
                SqlDataReader reader = CommandObj.ExecuteReader();
                while (reader.Read())
                {
                    return true;
                }
            }
            catch (Exception)
            {
                throw new Exception("job location checking failed, Server Error !");
            }
            finally
            {
                if (ConnectionObj != null && ConnectionObj.State == ConnectionState.Open)
                {
                    ConnectionObj.Close();
                }

            }
            return false;
        }


        public string UpdateJobLocation(Location location)
        {
            try
            {
                try
                {
                    ConnectionObj.Open();
                    string update = String.Format("Update tblJobLocation set Country='{0}' and city='{1}' where Id='{2}'", location.Country, location.City,location.Id);
                    CommandObj.CommandText =update;
                    CommandObj.ExecuteNonQuery();
                }
                catch (Exception)
                {
                    throw new Exception("job location update failed, Server Error !");
                }
                finally
                {
                    if (ConnectionObj != null && ConnectionObj.State == ConnectionState.Open)
                    {
                        ConnectionObj.Close();
                    }

                }
                return "Job location Update Successfull";
            }
            catch (Exception)
            {
                
                throw;
            }
        }
    }
}